package com.lhy.controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.lhy.domain.Page;
import com.lhy.domain.Result;
import com.lhy.domain.Friend;
import com.lhy.domain.Student;
import com.lhy.service.IUserService;

@Controller
@RequestMapping("/friendlist")
public class FriendListController {
	@Autowired
	private IUserService userService;
	

	@RequestMapping("")
	public ModelAndView list(HttpSession s,String pageNow){
		Integer pagenow = 1;
		if(!StringUtils.isEmpty(pageNow)){
			pagenow = Integer.valueOf(pageNow);
		}
		ModelAndView mv = new ModelAndView();
		try {
			Integer sequence = Integer.valueOf(s.getAttribute("usersequence").toString());
			Integer count = userService.queryAllFriendCount(sequence);
			Page page = new Page(count, pagenow);
			Integer startIndex = pagenow*page.getPageSize()-7;
			Integer endIndex = pagenow*page.getPageSize();
			List<Friend> list = userService.queryAllFriendByPage(sequence,startIndex,endIndex);

			mv.addObject("list",list);
			mv.addObject("page",page);
			mv.setViewName("friendlist");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}

	
	
	@RequestMapping("/deleteFriend")
	public @ResponseBody
	Map<String, Object> deleteFriend(String sequence,String usersequence){
		Map<String, Object> returnMap = new HashMap<>();
		Result result=new Result();
		result.setCode("1");
		try{
			userService.deleteFriend(Integer.valueOf(usersequence), Integer.valueOf(sequence));
			userService.deleteFriend(Integer.valueOf(sequence), Integer.valueOf(usersequence));
			if(Integer.valueOf(sequence)>Integer.valueOf(usersequence)){
				userService.deleteChatTable(Integer.valueOf(usersequence),Integer.valueOf(sequence));
			}else{
				userService.deleteChatTable(Integer.valueOf(sequence),Integer.valueOf(usersequence));
			}
			result.setDes("成功");
		}catch(Exception e){
			result.setCode("0");
			result.setDes(e.getMessage());
		}
		returnMap.put("result", result);
		return returnMap;
		
	}
	
}
